package dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import utile.JdbcUtile;


public abstract class basedao {
private QueryRunner queryrunner=new QueryRunner();
public int updata(String sql,Object ...params) throws Exception{
	Connection conn = JdbcUtile.getConnection();
	
	try {
		return queryrunner.update(conn, sql, params);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		JdbcUtile.close(conn);
	}
	return -1;
}
public <T>T queryforone(Class<T> type,String sql,Object ...params) throws Exception{
	Connection conn=JdbcUtile.getConnection();
	try {
		return queryrunner.query(conn, sql,new BeanHandler<T>(type), params);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		JdbcUtile.close(conn);
	}
	return null;
}
public <T>List<T> queryforlist(Class<T> type,String sql,Object ...params) throws Exception{
	Connection conn=JdbcUtile.getConnection();
	try {
		return queryrunner.query(conn, sql,new BeanListHandler<T>(type), params);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		JdbcUtile.close(conn);
	}
	return null;
}
public Object singlevalue(String sql,Object ...params) throws Exception{
	Connection conn=JdbcUtile.getConnection();
	try {
		return queryrunner.query(conn,sql,new ScalarHandler(),params);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		JdbcUtile.close(conn);
	}
	return null;
}

}

